<?php
/**
 * Created by PhpStorm.
 * User: THUNDEROBOT
 * Date: 2021/1/11
 * Time: 14:19
 */

namespace app\admin\controller;

use app\admin\model\Question;
use think\Controller;
use think\Request;
use think\Config;

class Questions extends Controller
{
    public function index(){
        $questionModel = new Question();
        $questionList = $questionModel->question();
//         dump($questionList->toArray());
        $emptyHtml = '<tr><td align="center" colspan="5">暂无任何数据</td></tr>';
         $this->assign("emptyHtml",$emptyHtml);
         $this->assign("questionList",$questionList);

        return $this->fetch();

    }

    //新增题目
    public function addquestion(){
        $questionModel = new Question();
        $questionList = $questionModel->getList();
        $this->assign("questionList",$questionList);
        return $this->fetch();
    }
    //提交新增题目
    public function doAddquestion(){
        if(Request::instance()->isPost()){
            $userData = Request::instance()->post();
//            dump($userData);

            $userModel = new Question();
            $res = $userModel->addQuestion($userData);
            if($res > 0){
                $this->success("新增成功","admin/Questions/index");
            }else{
                $this->error("新增失败");
            }
        }else{
            $this->error("操作失败");
        }
    }

    //编辑题目
    public function editquestion(){
        if(Request::instance()->isGet()){
            $id = Request::instance()->get("id");
            $userModel = new Question();
            $Data = $userModel->editquestion($id);
            $this->assign("id",$Data);
            $this->assign("question",$Data);
            $this->assign("answer",$Data);
        }
        return $this->fetch("editquestion");
    }

    //提交编辑
    public function doEdit(){
        if(Request::instance()->isPost()){
            $userDate = Request::instance()->post();
//            dump($userDate);
            $userModel = new Question();
            $re = $userModel->doEdit($userDate);
            if($re){
                $this->success("修改成功",'admin/questions/index');
            }else{
                $this->error("修改失败");
            }
        }
    }

    //删除题目
    public function deletequestion(){
        Config::set("default_return_type","json");
        $request = Request::instance();
        if($request->isAjax()){
            $rid = $request->post("rid");
            $userModel = new Question();
            $res = $userModel->deleteQuestion($rid);
            if($res == true){
                return array("flag"=>1,"msg"=>"删除成功");

            }else{
                return array("flag"=>0,"msg"=>"删除失败");
            }
        }
        else{
            return array("flag"=>0,"msg"=>"操作失败");
        }
    }

}